﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.DataModel
{
	public class HYTagOperate
	{
		public HYTagOperate()
		{
			if (!File.Exists(this.ductSetPath))
			{
				File.Copy(Product.InstallLocation + "\\Data\\HYMarkCheck.xml", this.ductSetPath, true);
			}
			this.xmlDoc = XMLOperating.LoadXml(this.ductSetPath);
		}

		public HYTagOperate(ExternalCommandData commandData)
		{
			this.exCmd = commandData;
			this.uiApp = commandData.Application;
			this.uiDoc = this.uiApp.ActiveUIDocument;
			this.doc = this.uiDoc.Document;
			this.sel = this.uiDoc.Selection;
		}

		public string GetTerminalImagePath(string name)
		{
			return string.Format(Product.InstallLocation + "\\Res\\dt{0}.png", name);
		}

		public void SetNodeValue(string childName, string valueName, string vValue)
		{
			try
			{
				(this.xmlDoc.FirstChild.NextSibling.SelectSingleNode(childName) as XmlElement).SetAttribute(valueName, vValue);
			}
			catch (Exception)
			{
			}
		}

		public string GetNodeValue(string childName, string cValue)
		{
			string result;
			try
			{
				result = (this.xmlDoc.FirstChild.NextSibling.SelectSingleNode(childName) as XmlElement).GetAttribute(cValue);
			}
			catch (Exception)
			{
				result = "";
			}
			return result;
		}

		public bool SaveXml()
		{
			bool result = false;
			try
			{
				this.xmlDoc.Save(this.ductSetPath);
				result = true;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public bool WHExchange(Duct duct)
		{
			IEnumerator enumerator = duct.ConnectorManager.Connectors.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((Connector)enumerator.Current).Shape != ConnectorSharp.Rect)
					{
						YJKMessageBox.Show("选择风管不是矩形！");
						return false;
					}
				}
			}
			int height = Convert.ToInt32(YJKRevitTools.GetDuctWidthorDiameter(duct) * 304.8);
			int width = Convert.ToInt32(YJKRevitTools.GetDuctHeightorDiameter(duct) * 304.8);
			return YJKRevitTools.ModifyDuctWidthHeight(duct, width, height);
		}

		public bool HYLoadFamily(string markType, string familyName)
		{
			string markFamilyPath = UnifiedModified.GetMarkFamilyPath(this.doc, familyName);
			return this.doc.LoadFamily(markFamilyPath);
		}

		private bool SetDuctInstanceParam()
		{
			return YJKRevitTools.SetInstanceParameter(this.uiApp, this.pathName, "墙体附加数据", "风管引线ID", BuiltInCategory.OST_DuctCurves, false);
		}

		private bool SetTerminalParam()
		{
			return YJKRevitTools.SetInstanceParameter(this.uiApp, this.pathName, "墙体附加数据", "风口引线ID", BuiltInCategory.OST_DuctTerminal, false);
		}

		private bool SetPipeParam()
		{
			return YJKRevitTools.SetInstanceParameter(this.uiApp, this.pathName, "墙体附加数据", "标注引线ID", BuiltInCategory.OST_PipeCurves, false);
		}

		private string GetTagName(List<string> listTag, string belowDirection, string offsetStyle)
		{
			string text = this.GetTagName(listTag, belowDirection);
			if (text.Contains("标高"))
			{
				if (!(offsetStyle == "顶部"))
				{
					if (!(offsetStyle == "中部"))
					{
						if (offsetStyle == "底部")
						{
							text += "底";
						}
					}
					else
					{
						text += "中";
					}
				}
				else
				{
					text += "顶";
				}
			}
			return text;
		}

		private string GetTagName(List<string> listTag, string belowDirection)
		{
			string text = "";
			if (!(belowDirection == "up"))
			{
				if (belowDirection == "down")
				{
					if (listTag.Contains("风管风速标注") && !listTag.Contains("风管流量标注"))
					{
						text = "风管风速";
					}
					else if (!listTag.Contains("风管风速标注") && listTag.Contains("风管流量标注"))
					{
						text = "风管风量";
					}
					else if (listTag.Contains("风管风速标注") && listTag.Contains("风管流量标注"))
					{
						text = "风量风速";
					}
					else if (string.IsNullOrEmpty(text))
					{
						text = "风管风量";
					}
				}
			}
			else if (listTag.Contains("风管系统缩写标注") && !listTag.Contains("风管偏移量标注") && !listTag.Contains("风管尺寸标注"))
			{
				text = "风管类型";
			}
			else if (!listTag.Contains("风管系统缩写标注") && listTag.Contains("风管偏移量标注") && !listTag.Contains("风管尺寸标注"))
			{
				text = "风管标高";
			}
			else if (!listTag.Contains("风管系统缩写标注") && !listTag.Contains("风管偏移量标注") && listTag.Contains("风管尺寸标注"))
			{
				text = "风管尺寸";
			}
			else if (listTag.Contains("风管系统缩写标注") && listTag.Contains("风管偏移量标注") && !listTag.Contains("风管尺寸标注"))
			{
				text = "系统标高";
			}
			else if (listTag.Contains("风管系统缩写标注") && listTag.Contains("风管偏移量标注") && listTag.Contains("风管尺寸标注"))
			{
				text = "系统标高尺寸";
			}
			else if (!listTag.Contains("风管系统缩写标注") && listTag.Contains("风管偏移量标注") && listTag.Contains("风管尺寸标注"))
			{
				text = "标高管径";
			}
			else if (listTag.Contains("风管系统缩写标注") && !listTag.Contains("风管偏移量标注") && listTag.Contains("风管尺寸标注"))
			{
				text = "系统尺寸";
			}
			else if (string.IsNullOrEmpty(text))
			{
				text = "风管尺寸";
			}
			return text;
		}

		private int GetElemTagElementId(HYTagOperate.MarkTagType markType, string tagName, string familyname)
		{
			string markFamilyPath = UnifiedModified.GetMarkFamilyPath(this.doc, familyname);
			int num = 0;
			try
			{
				foreach (Element element in this.GetTagElement(markType))
				{
					if (element.Name == tagName)
					{
						num = element.Id.IntegerValue;
					}
				}
				if (num == 0)
				{
					this.doc.LoadFamily(markFamilyPath);
					foreach (Element element2 in this.GetTagElement(markType))
					{
						if (element2.Name == tagName)
						{
							num = element2.Id.IntegerValue;
						}
					}
				}
				if (num == 0)
				{
					FamilySymbol familySymbol = null;
					if (this.doc.LoadFamilySymbol(markFamilyPath, tagName, out familySymbol) && familySymbol != null)
					{
						num = familySymbol.Id.IntegerValue;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return num;
		}

		private int GetElemTagElementIdOvver(HYTagOperate.MarkTagType markType, string tagName, string familyname)
		{
			string markFamilyPath = UnifiedModified.GetMarkFamilyPath(this.doc, familyname);
			int num = 0;
			try
			{
				foreach (Element element in this.GetTagElement(markType))
				{
					if (element.Name == tagName)
					{
						num = element.Id.IntegerValue;
					}
				}
				if (num == 0)
				{
					this.doc.LoadFamily(markFamilyPath);
					foreach (Element element2 in this.GetTagElement(markType))
					{
						if (element2.Name == tagName)
						{
							num = element2.Id.IntegerValue;
						}
					}
				}
				if (num == 0)
				{
					FamilySymbol familySymbol = null;
					if (this.doc.LoadFamilySymbol(markFamilyPath, tagName, new FamilyOptions(), out familySymbol) && familySymbol != null)
					{
						num = familySymbol.Id.IntegerValue;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return num;
		}

		private int GetElemTagElementId(HYTagOperate.MarkTagType markType, string tagName)
		{
			string markTagPath = UnifiedModified.GetMarkTagPath(markType.ToString());
			int num = 0;
			try
			{
				foreach (Element element in this.GetTagElement(markType))
				{
					if (element.Name == tagName)
					{
						num = element.Id.IntegerValue;
					}
				}
				if (num == 0)
				{
					this.doc.LoadFamily(markTagPath + tagName + ".rfa");
					foreach (Element element2 in this.GetTagElement(markType))
					{
						if (element2.Name == tagName)
						{
							num = element2.Id.IntegerValue;
						}
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return num;
		}

		private void LoadElemTag(HYTagOperate.MarkTagType markType, string tagName)
		{
			UnifiedModified.GetMarkTagPath(markType.ToString());
			int num = 0;
			try
			{
				foreach (Element element in this.GetTagElement(markType))
				{
					if (element.Name == tagName)
					{
						num = element.Id.IntegerValue;
					}
				}
				if (num == 0)
				{
					switch (markType)
					{
					case HYTagOperate.MarkTagType.duct:
						this.doc.LoadFamily(UnifiedModified.GetMarkFamilyPath(this.doc, "HY_风管标注"));
						break;
					case HYTagOperate.MarkTagType.pipe:
						this.doc.LoadFamily(UnifiedModified.GetMarkFamilyPath(this.doc, "HY_水管标注"));
						break;
					case HYTagOperate.MarkTagType.terminal:
						this.doc.LoadFamily(UnifiedModified.GetMarkFamilyPath(this.doc, "HY_风口标注"));
						break;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		private FilteredElementCollector GetTagElement(HYTagOperate.MarkTagType markType)
		{
			FilteredElementCollector result = new FilteredElementCollector(this.doc);
			switch (markType)
			{
			case HYTagOperate.MarkTagType.duct:
				result = YJKElementFilter.GetElementsOfType(this.doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctTags);
				break;
			case HYTagOperate.MarkTagType.pipe:
				result = YJKElementFilter.GetElementsOfType(this.doc, typeof(FamilySymbol), BuiltInCategory.OST_PipeTags);
				break;
			case HYTagOperate.MarkTagType.terminal:
				result = YJKElementFilter.GetElementsOfType(this.doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctTerminalTags);
				break;
			}
			return result;
		}

		private ElementNode InitialSearch(HYTagOperate.MarkTagType markType)
		{
			SearchBranch searchBranch = null;
			switch (markType)
			{
			case HYTagOperate.MarkTagType.duct:
            searchBranch = new SearchBranch(YArchitech.Revit.ElementType.Duct, this.uiDoc);
				searchBranch.PickFirstElement(new DuctFilter(), "请选择要标注风管系统的入口风管(注:距选择点近的接口为搜索的入口)……");
				break;
			case HYTagOperate.MarkTagType.pipe:
                searchBranch = new SearchBranch(YArchitech.Revit.ElementType.Pipe, this.uiDoc);
				searchBranch.PickFirstElement(new PipeFilter(), "请选择要标注水管系统的入口水管(注:距选择点近的接口为搜索的入口)……");
				break;
			case HYTagOperate.MarkTagType.terminal:
				return null;
			}
			searchBranch.DoSearch();
			return searchBranch.GetListRoot();
		}

		private ElementNode InitialSearch(string type)
		{
			SearchBranch searchBranch = null;
			if (!(type == "风管"))
			{
				if (type == "水管")
				{
					Reference reference = this.uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择要标注水管系统的入口水管(注:距选择点近的接口为搜索的入口)……");
					searchBranch = new SearchBranch(this.doc.GetElement(reference));
				}
			}
			else
			{
				Reference reference2 = this.uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DuctFilter(), "请选择要标注风管系统的入口风管(注:距选择点近的接口为搜索的入口)……");
				searchBranch = new SearchBranch(this.doc.GetElement(reference2));
			}
			searchBranch.DoSearch();
			return searchBranch.GetListRoot();
		}

		private List<ElementNode> GetAllElemNode(ElementNode eNode, HYTagOperate.MarkTagType markType, ref List<ElementNode> listENode)
		{
			if (eNode == null)
			{
				return listENode;
			}
			switch (markType)
			{
			case HYTagOperate.MarkTagType.duct:
				if (eNode.Element is Duct)
				{
					listENode.Add(eNode);
				}
				break;
			case HYTagOperate.MarkTagType.pipe:
				if (eNode.Element is Pipe)
				{
					listENode.Add(eNode);
				}
				break;
			case HYTagOperate.MarkTagType.terminal:
				return null;
			}
			this.GetAllElemNode(eNode.Next, markType, ref listENode);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetAllElemNode(eNode2, markType, ref listENode);
				}
			}
			return listENode;
		}

		public void MarkDuctTag(string markModel, string markStyle, List<string> listTagName, double distancToDuct, double minLength, string offsetStyle, bool isDelete)
		{
			RevitVersionFuncs.ClearSelection(this.sel);
			this.SetDuctInstanceParam();
			this.HYLoadFamily(HYTagOperate.MarkTagType.duct.ToString(), "HY_风管标注");
			if (markModel == "Auto")
			{
				this.MarkAutoDuct(listTagName, distancToDuct, minLength, markStyle, offsetStyle, isDelete);
				return;
			}
			if (!(markModel == "Man"))
			{
				return;
			}
			this.ManMarkDuctTag(markStyle, listTagName, distancToDuct, offsetStyle, isDelete);
		}

		public IList<IndependentTag> MarkAutoDuct(List<string> tagName, double tagDis, double minLength, string markStyle, string offsetStyle, bool isDelete)
		{
			IList<IndependentTag> result;
			try
			{
				ElementNode eNode = this.InitialSearch("风管");
				List<ElementNode> list = new List<ElementNode>();
				list = this.GetAllElemNode(eNode, HYTagOperate.MarkTagType.duct, ref list);
				IList<IndependentTag> list2 = new List<IndependentTag>();
				new XYZ(0.0, 0.0, 0.0);
				new XYZ(0.0, 0.0, 0.0);
				new XYZ(0.0, 0.0, 0.0);
				new XYZ(0.0, 0.0, 0.0);
				new XYZ(0.0, 0.0, 0.0);
				new XYZ(0.0, 0.0, 0.0);
				foreach (ElementNode elementNode in list)
				{
					new ElementSet();
					Duct duct = elementNode.Element as Duct;
					XYZ elementCurve = YJKRevitTools.GetElementCurve(duct, 0);
					XYZ elementCurve2 = YJKRevitTools.GetElementCurve(duct, 1);
					if (Math.Abs(elementCurve.Z - elementCurve2.Z) <= 0.001)
					{
						YJKRevitTools.SetSystemSimple(this.doc, duct);
						if (isDelete)
						{
							YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_DuctTags, duct);
							YJKRevitTools.DeleteDLGroups(this.doc, duct, "风管引线ID");
						}
						if (elementCurve.DistanceTo(elementCurve2) >= minLength / 304.8)
						{
							XYZ positiveHVector = this.GetPositiveHVector(duct);
							XYZ xyz = YJKRevitTools.GetVectorByHorizontal(positiveHVector);
							XYZ xyz2;
							XYZ xyz3;
							if (elementNode.ConnectorIn != null && elementNode.ConnectorOut != null)
							{
								xyz2 = elementNode.ConnectorIn.Origin;
								xyz3 = elementNode.ConnectorOut.Origin;
							}
							else if (elementNode.ConnectorIn != null)
							{
								xyz2 = elementNode.ConnectorIn.Origin;
								xyz3 = new YJKPiping<Duct>(duct).GetDistancePoint(xyz2);
							}
							else if (elementNode.ConnectorOut != null)
							{
								xyz3 = elementNode.ConnectorOut.Origin;
								xyz2 = new YJKPiping<Duct>(duct).GetDistancePoint(xyz3);
							}
							else
							{
								YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
								xyz2 = hypiping.StartConnector.Origin;
								xyz3 = hypiping.EndConnector.Origin;
							}
							if (YJKRevitTools.JudgePointLineSite(xyz2 + 2.0 * xyz, xyz2, xyz3) != "左")
							{
								xyz = -1.0 * xyz;
							}
							XYZ xyz4 = new XYZ(0.0, 1.0, 0.0);
							if (xyz.AngleTo(xyz4) - xyz.AngleTo(-xyz4) > 0.001)
							{
								xyz = -1.0 * xyz;
							}
							new List<XYZ>();
							double num = YJKRevitTools.GetDuctWidthorDiameter(elementNode.Element as Duct) * 0.5 + (200.0 + tagDis) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
							List<string> list3;
							if (!(markStyle == "管线上"))
							{
								if (!(markStyle == "管线中"))
								{
									if (markStyle == "正交引线上")
									{
										goto IL_55C;
									}
									if (markStyle == "引线上")
									{
										list3 = new List<string>();
										list3.Insert(0, this.GetTagName(tagName, "up", offsetStyle));
										this.CreatAutoTag(list3, elementNode, positiveHVector, ref list2, num);
										continue;
									}
									if (markStyle == "正交引线中")
									{
										list3 = new List<string>();
										list3.Insert(0, this.GetTagName(tagName, "up", offsetStyle));
										list3.Insert(1, this.GetTagName(tagName, "down"));
										this.CreatAutoTag(list3, elementNode, positiveHVector, ref list2, num + 1.3123359580052494);
										continue;
									}
									if (!(markStyle == "引线中"))
									{
										continue;
									}
									list3 = new List<string>();
									list3.Insert(0, this.GetTagName(tagName, "up", offsetStyle));
									list3.Insert(1, this.GetTagName(tagName, "down"));
									this.CreatAutoTag(list3, elementNode, positiveHVector, ref list2, num + 1.3123359580052494);
									continue;
								}
							}
							else
							{
								IEnumerator<XYZ> enumerator2 = this.GetTagPoint(elementNode, num, positiveHVector, xyz, "上").GetEnumerator();
								{
									while (enumerator2.MoveNext())
									{
										XYZ xyz5 = enumerator2.Current;
										IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(duct), false, 0, 0, xyz5);
										independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.duct, this.GetTagName(tagName, "up", offsetStyle), "HY_风管标注")));
										XYZ tagHeadPosition = xyz5 - (double)(StringTools.GetStringByteLength(independentTag.TagText) * 75) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * positiveHVector;
										independentTag.TagHeadPosition = tagHeadPosition;
										list2.Add(independentTag);
									}
									continue;
								}
							}
							IEnumerator<XYZ> enumerator3 = this.GetTagPoint(elementNode, positiveHVector).GetEnumerator();
							{
								while (enumerator3.MoveNext())
								{
									XYZ xyz6 = enumerator3.Current;
									IndependentTag independentTag2 = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(duct), false, 0, 0, xyz6);
									independentTag2.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.duct, this.GetTagName(tagName, "up", offsetStyle), "HY_风管标注")));
									XYZ tagHeadPosition2 = xyz6 - (double)(StringTools.GetStringByteLength(independentTag2.TagText) * 75) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * positiveHVector;
									independentTag2.TagHeadPosition = tagHeadPosition2;
									list2.Add(independentTag2);
								}
								continue;
							}
							IL_55C:
							list3 = new List<string>();
							list3.Insert(0, this.GetTagName(tagName, "up", offsetStyle));
							this.CreatAutoTag(list3, elementNode, positiveHVector, ref list2, num);
						}
					}
				}
				if (list2.Count == 0)
				{
					YJKMessageBox.Information("管线未被标注！\n搜索到的管线，管长或管径小于最小标注值！");
				}
				result = list2;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private void ManMarkDuctTag(string markStyle, List<string> listTagName, double tagDis, string offsetStyle, bool isDelete)
		{
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new NRDuctFilter(), "请选择要标注的风管……");
			Duct duct = this.doc.GetElement(reference) as Duct;
			YJKRevitTools.SetSystemSimple(this.doc, duct);
			if (isDelete)
			{
				YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_DuctTags, duct);
				YJKRevitTools.DeleteDLGroups(this.doc, duct, "风管引线ID");
			}
			Curve elementCurve = this.GetElementCurve(duct);
			double num = (double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0;
			XYZ positiveVVector = this.GetPositiveVVector(duct);
			XYZ positiveHVector = this.GetPositiveHVector(duct);
			XYZ xyz = elementCurve.Project(reference.GlobalPoint).XYZPoint;
			xyz = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ xyz2 = new XYZ(0.0, 0.0, 0.0);
			string tagName = this.GetTagName(listTagName, "up", offsetStyle);
			XYZ pickPoint = new XYZ(0.0, 0.0, 0.0);
			new XYZ(0.0, 0.0, 0.0);
			new ElementSet();
			duct.GetParameter("风管引线ID");
			IList<string> list = new List<string>();
			IList<string> list2 = new List<string>();
			if (markStyle == "管线上")
			{
				xyz2 = xyz + (YJKRevitTools.GetDuctWidthorDiameter(duct) * 0.5 + 0.65616797900262469) * num * positiveVVector;
				IndependentTag independentTag = this.CreatElementTag(duct, xyz2, tagName, HYTagOperate.MarkTagType.duct, "HY_风管标注");
				XYZ tagHeadPosition = xyz2 - (double)(StringTools.GetStringByteLength(independentTag.TagText) * 75) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * positiveHVector;
				independentTag.TagHeadPosition = tagHeadPosition;
				return;
			}
			if (markStyle == "管线中")
			{
				xyz2 = xyz;
				IndependentTag independentTag = this.CreatElementTag(duct, xyz2, tagName, HYTagOperate.MarkTagType.duct, "HY_风管标注");
				XYZ tagHeadPosition = xyz2 - (double)(StringTools.GetStringByteLength(independentTag.TagText) * 75) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * positiveHVector;
				independentTag.TagHeadPosition = tagHeadPosition;
				return;
			}
			if (markStyle == "正交引线上")
			{
				pickPoint = this.uiDoc.Selection.PickPoint("请选择标注的方位点……");
				list = new List<string>();
				list.Insert(0, this.GetTagName(listTagName, "up", offsetStyle));
				this.CreatTagEblowUP(duct, positiveVVector, positiveHVector, list, pickPoint);
				return;
			}
			if (markStyle == "引线上")
			{
				pickPoint = this.uiDoc.Selection.PickPoint("请选择标注的方位点……");
				list2 = new List<string>();
				list2.Insert(0, this.GetTagName(listTagName, "up", offsetStyle));
				this.CreatTagLeadLineUP(reference, positiveVVector, positiveHVector, list2, pickPoint);
				return;
			}
			if (markStyle == "正交引线中")
			{
				pickPoint = this.uiDoc.Selection.PickPoint("请选择标注的方位点……");
				list = new List<string>();
				list.Insert(0, this.GetTagName(listTagName, "up", offsetStyle));
				list.Insert(1, this.GetTagName(listTagName, "down"));
				this.CreatTagEblowUP(duct, positiveVVector, positiveHVector, list, pickPoint);
				return;
			}
			if (!(markStyle == "引线中"))
			{
				return;
			}
			pickPoint = this.uiDoc.Selection.PickPoint("请选择标注的方位点……");
			list2 = new List<string>();
			list2.Insert(0, this.GetTagName(listTagName, "up", offsetStyle));
			list2.Insert(1, this.GetTagName(listTagName, "down"));
			this.CreatTagLeadLineUP(reference, positiveVVector, positiveHVector, list2, pickPoint);
		}

		private void CreatAutoTag(List<string> listTagName, ElementNode eNode, XYZ hVec, ref IList<IndependentTag> listTag, double tagDis)
		{
			Duct duct = eNode.Element as Duct;
			XYZ positiveVVector = this.GetPositiveVVector(duct);
			IEnumerable<XYZ> tagPoint = this.GetTagPoint(eNode, tagDis, hVec, positiveVVector, "上");
			XYZ positiveHVector = this.GetPositiveHVector(duct);
			ElementSet elementSet = new ElementSet();
			foreach (XYZ xyz in tagPoint)
			{
				IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(duct), false, 0, 0, xyz);
				independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.duct, listTagName[0], "HY_风管标注")));
				double num = (double)(StringTools.GetStringByteLength(independentTag.TagText) * 70) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
				XYZ xyz2 = xyz - 0.65616797900262469 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * positiveVVector;
				XYZ xyz3 = xyz - 1.3123359580052494 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * positiveVVector;
				IndependentTag independentTag2 = null;
				if (listTagName.Count > 1 && !string.IsNullOrEmpty(listTagName[1]))
				{
					independentTag2 = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(duct), false, 0, 0, xyz3);
					independentTag2.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.duct, listTagName[1], "HY_风管标注")));
					double num2 = (double)(StringTools.GetStringByteLength(independentTag2.TagText) * 70) / 304.8 * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
					num = ((num > num2) ? num : num2);
				}
				XYZ tagHeadPosition = xyz - num * positiveHVector;
				independentTag.TagHeadPosition = tagHeadPosition;
				listTag.Add(independentTag);
				if (listTagName.Count > 1 && !string.IsNullOrEmpty(listTagName[1]))
				{
					tagHeadPosition = xyz3 - num * positiveHVector;
					independentTag2.TagHeadPosition = tagHeadPosition;
					listTag.Add(independentTag2);
				}
				XYZ xyz4 = xyz2 - num * positiveHVector;
				xyz4 = new XYZ(xyz4.X, xyz4.Y, 0.0);
				XYZ xyz5 = xyz2 + num * positiveHVector;
				xyz5 = new XYZ(xyz5.X, xyz5.Y, 0.0);
				XYZ xyz6 = this.GetProPointToCurve(duct, xyz4);
				xyz6 = new XYZ(xyz6.X, xyz6.Y, 0.0);
				elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz4, xyz5));
				elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz4, xyz6));
			}
			this.GetDuctPara(elementSet, duct);
		}

		private void CreatTagEblowUP(Duct duct, XYZ vVector, XYZ hVector, IList<string> listTagName, XYZ pickPoint)
		{
			double num = (double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0;
			double num2 = (double)StringTools.GetStringByteLength(this.CreatElementTag(duct, pickPoint, listTagName[0], HYTagOperate.MarkTagType.duct, "HY_风管标注").TagText);
			if (listTagName.Count > 1 && !string.IsNullOrEmpty(listTagName[1]))
			{
				XYZ markPoint = pickPoint - 1.3123359580052494 * num * vVector;
				double num3 = (double)StringTools.GetStringByteLength(this.CreatElementTag(duct, markPoint, listTagName[1], HYTagOperate.MarkTagType.duct, "HY_风管标注").TagText);
				num2 = ((num2 > num3) ? num2 : num3);
			}
			ElementSet elementSet = new ElementSet();
			XYZ xyz = pickPoint - 0.52493438320209973 * num * vVector + num2 * 140.0 / 304.8 * num * hVector;
			XYZ xyz2 = pickPoint - 0.52493438320209973 * num * vVector;
			XYZ xyz3 = this.GetProPointToCurve(duct, xyz2);
			xyz3 = new XYZ(xyz3.X, xyz3.Y, 0.0);
			xyz = new XYZ(xyz.X, xyz.Y, 0.0);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz, xyz2));
			elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz2, xyz3));
			this.GetDuctPara(elementSet, duct);
		}

		private void CreatTagLeadLineUP(Reference refer, XYZ vVector, XYZ hVector, IList<string> listTagName, XYZ pickPoint)
		{
			Duct duct = this.doc.GetElement(refer) as Duct;
			XYZ xyz = this.GetProPointToCurve(duct, refer.GlobalPoint);
			xyz = new XYZ(xyz.X, xyz.Y, 0.0);
			double num = (double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0;
			double num2 = (double)StringTools.GetStringByteLength(this.CreatElementTag(duct, pickPoint, listTagName[0], HYTagOperate.MarkTagType.duct, "HY_风管标注").TagText);
			if (listTagName.Count > 1 && !string.IsNullOrEmpty(listTagName[1]))
			{
				XYZ markPoint = pickPoint - 1.3123359580052494 * num * vVector;
				double num3 = (double)StringTools.GetStringByteLength(this.CreatElementTag(duct, markPoint, listTagName[1], HYTagOperate.MarkTagType.duct, "HY_风管标注").TagText);
				num2 = ((num2 > num3) ? num2 : num3);
			}
			ElementSet elementSet = new ElementSet();
			XYZ xyz2 = pickPoint - 0.59055118110236215 * num * vVector + num2 * 140.0 / 304.8 * num * hVector;
			XYZ xyz3 = pickPoint - 0.59055118110236215 * num * vVector;
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			xyz3 = new XYZ(xyz3.X, xyz3.Y, 0.0);
			elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz2, xyz3));
			elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz3, xyz));
			this.GetDuctPara(elementSet, duct);
		}

		private bool GetDuctPara(ElementSet elemSet, Duct duct)
		{
			Group group = this.doc.CreatYJKGroup(elemSet);
			Parameter parameter = duct.GetParameter("风管引线ID");
			string text = parameter.AsString() + "*" + group.Id.ToString();
			parameter.Set(text);
			return true;
		}

		public void MarkTerminalTag(bool beHaveFlow, bool bHasPrefix)
		{
			RevitVersionFuncs.ClearSelection(this.sel);
			DuctTerminalFilter ductTerminalFilter = new DuctTerminalFilter();
			this.SetTerminalParam();
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, ductTerminalFilter, "请选择要标注的风口……");
			FamilyInstance familyInstance = this.doc.GetElement(reference) as FamilyInstance;
			YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_DuctTerminalTags, familyInstance);
			YJKRevitTools.DeleteDLGroups(this.doc, familyInstance, "风口引线ID");
			Parameter parameter = familyInstance.GetParameter("风口引线ID");
			XYZ point = (familyInstance.Location as LocationPoint).Point;
			XYZ xyz = this.sel.PickPoint((Autodesk.Revit.UI.Selection.ObjectSnapTypes)4, "选择标注创建点");
			xyz = new XYZ(xyz.X, xyz.Y, point.Z);
			XYZ xyz2 = new XYZ(0.0, 1.0, 0.0);
			XYZ xyz3 = new XYZ(1.0, 0.0, 0.0);
			double num = (double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0;
			XYZ xyz4 = xyz;
			IndependentTag independentTag = null;
			IndependentTag independentTag2;
			int num2;
			IndependentTag independentTag3;
			if (beHaveFlow)
			{
				if (bHasPrefix)
				{
					independentTag2 = this.CreatElementTag(familyInstance, xyz4, "前缀尺寸", HYTagOperate.MarkTagType.terminal);
				}
				else
				{
					independentTag2 = this.CreatElementTag(familyInstance, xyz4, "风口尺寸", HYTagOperate.MarkTagType.terminal);
				}
				num2 = StringTools.GetStringByteLength(independentTag2.TagText);
				xyz4 = xyz + 400.0 * num / 304.8 * xyz2;
				independentTag3 = this.CreatElementTag(familyInstance, xyz4, "风口名称", HYTagOperate.MarkTagType.terminal);
				num2 = ((num2 > StringTools.GetStringByteLength(independentTag3.TagText)) ? num2 : StringTools.GetStringByteLength(independentTag3.TagText));
				xyz4 = xyz - 400.0 * num / 304.8 * xyz2;
				independentTag = this.CreatElementTag(familyInstance, xyz4, "风口风量", HYTagOperate.MarkTagType.terminal);
				num2 = ((num2 > StringTools.GetStringByteLength(independentTag.TagText)) ? num2 : StringTools.GetStringByteLength(independentTag.TagText));
			}
			else
			{
				if (bHasPrefix)
				{
					independentTag2 = this.CreatElementTag(familyInstance, xyz4, "前缀尺寸", HYTagOperate.MarkTagType.terminal);
				}
				else
				{
					independentTag2 = this.CreatElementTag(familyInstance, xyz4, "风口尺寸", HYTagOperate.MarkTagType.terminal);
				}
				num2 = StringTools.GetStringByteLength(independentTag2.TagText);
				xyz4 = xyz + 400.0 * num / 304.8 * xyz2;
				independentTag3 = this.CreatElementTag(familyInstance, xyz4, "风口名称", HYTagOperate.MarkTagType.terminal);
				num2 = ((num2 > StringTools.GetStringByteLength(independentTag3.TagText)) ? num2 : StringTools.GetStringByteLength(independentTag3.TagText));
			}
			if (xyz.X < point.X)
			{
				xyz4 = xyz - (double)(num2 * 75) / 304.8 * num * xyz3;
			}
			else
			{
				xyz4 = xyz + (double)(num2 * 75) / 304.8 * num * xyz3;
			}
			List<double> ductTerminalTagSize = UnifiedModified.GetDuctTerminalTagSize();
			if (beHaveFlow)
			{
				independentTag.TagHeadPosition = xyz4 - ductTerminalTagSize[0] * num / 304.8 * xyz2;
				independentTag2.TagHeadPosition = xyz4 + ductTerminalTagSize[0] * num / 304.8 * xyz2;
				independentTag3.TagHeadPosition = xyz4 + ductTerminalTagSize[1] * num / 304.8 * xyz2;
			}
			else
			{
				independentTag2.TagHeadPosition = xyz4 - ductTerminalTagSize[0] * num / 304.8 * xyz2;
				independentTag3.TagHeadPosition = xyz4 + ductTerminalTagSize[0] * num / 304.8 * xyz2;
			}
			ElementSet elementSet = new ElementSet();
			elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, point, xyz));
			if (xyz.X < point.X)
			{
				XYZ ePoint = xyz - (double)(num2 * 150) / 304.8 * num * xyz3;
				elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz, ePoint));
			}
			else
			{
				XYZ ePoint = xyz + (double)(num2 * 150) / 304.8 * num * xyz3;
				elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz, ePoint));
			}
			Group group = this.doc.CreatYJKGroup(elementSet);
			parameter.Set(group.Id.ToString());
		}

		public IndependentTag MarkSinglePipe(string tagName, double distancToPipe, string upMiddleDown, bool isDelete)
		{
			IndependentTag result;
			try
			{
				Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择要标注的水管……");
				Pipe pipe = this.doc.GetElement(reference) as Pipe;
				YJKRevitTools.SetSystemSimple(this.doc, pipe);
				if (isDelete)
				{
					YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, pipe, this.szPipeText, false);
					YJKRevitTools.DeleteDLGroups(this.doc, pipe, "标注引线ID", true);
				}
				LocationCurve locationCurve = pipe.Location as LocationCurve;
				XYZ xyzpoint = locationCurve.Curve.Project(reference.GlobalPoint).XYZPoint;
				XYZ xyz = YJKRevitTools.GetVectorByHorizontal((locationCurve.Curve.GetEndPoint(1) - locationCurve.Curve.GetEndPoint(0)).Normalize());
				if (xyzpoint.Y > (xyzpoint + xyz * 2.0).Y)
				{
					xyz = -1.0 * xyz;
				}
				double num = (pipe.Diameter / 2.0 + (distancToPipe + 125.0) / 304.8) * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
				XYZ pnt = null;
				if (upMiddleDown == "上")
				{
					pnt = xyzpoint + num * xyz;
				}
				else if (upMiddleDown == "中")
				{
					pnt = xyzpoint;
				}
				else if (upMiddleDown == "下")
				{
					pnt = xyzpoint - num * xyz;
				}
				IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), false, 0, 0, pnt);
				independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName, "HY_水管标注")));
				result = independentTag;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public IList<IndependentTag> MarkAutoPipe(string tagName, double distancToPipe, double distanceToTag, string upMiddleDown)
		{
			IList<IndependentTag> result;
			try
			{
				ElementNode eNode = this.InitialSearch(HYTagOperate.MarkTagType.pipe);
				List<ElementNode> list = new List<ElementNode>();
				list = this.GetAllElemNode(eNode, HYTagOperate.MarkTagType.pipe, ref list);
				IList<IndependentTag> list2 = new List<IndependentTag>();
				foreach (ElementNode elementNode in list)
				{
					Pipe pipe = elementNode.Element as Pipe;
					if (!this.IsVerticalByView(pipe))
					{
						YJKRevitTools.SetSystemSimple(this.doc, pipe);
						double tagDis = (pipe.Diameter / 2.0 + distancToPipe / 304.8) * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
						YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, pipe, this.szPipeText, false);
						XYZ hVector = (elementNode.ConnectorOut.Origin - elementNode.ConnectorIn.Origin).Normalize();
						XYZ xyz = YJKRevitTools.GetVectorByHorizontal(hVector);
						XYZ xyz2;
						XYZ xyz3;
						if (elementNode.ConnectorIn != null && elementNode.ConnectorOut != null)
						{
							xyz2 = elementNode.ConnectorIn.Origin;
							xyz3 = elementNode.ConnectorOut.Origin;
						}
						else if (elementNode.ConnectorIn != null)
						{
							xyz2 = elementNode.ConnectorIn.Origin;
							xyz3 = new YJKPiping<Pipe>(pipe).GetDistancePoint(xyz2);
						}
						else if (elementNode.ConnectorOut != null)
						{
							xyz3 = elementNode.ConnectorOut.Origin;
							xyz2 = new YJKPiping<Pipe>(pipe).GetDistancePoint(xyz3);
						}
						else
						{
							YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
							xyz2 = hypiping.StartConnector.Origin;
							xyz3 = hypiping.EndConnector.Origin;
						}
						if (YJKRevitTools.JudgePointLineSite(xyz2 + 2.0 * xyz, xyz2, xyz3) != "左")
						{
							xyz = -1.0 * xyz;
						}
						XYZ xyz4 = new XYZ(0.0, 1.0, 0.0);
						if (xyz.AngleTo(xyz4) > xyz.AngleTo(-xyz4))
						{
							xyz = -1.0 * xyz;
						}
						foreach (XYZ pnt in this.GetTagPoint(elementNode, distanceToTag / 304.8, tagDis, hVector, xyz, upMiddleDown))
						{
							IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), false, 0, 0, pnt);
							independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName)));
							list2.Add(independentTag);
						}
					}
				}
				result = list2;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public IList<IndependentTag> MarkAutoPipe(string tagName, double distancToPipe, string upMiddleDown, bool beDelTag, bool isMarkbyBJ, double minLength, double minDia = 8.0)
		{
			IList<IndependentTag> result;
			try
			{
				ElementNode eNode = this.InitialSearch("水管");
				List<ElementNode> list = new List<ElementNode>();
				list = this.GetAllElemNode(eNode, HYTagOperate.MarkTagType.pipe, ref list);
				IList<IndependentTag> list2 = new List<IndependentTag>();
				foreach (ElementNode elementNode in list)
				{
					Pipe pipe = elementNode.Element as Pipe;
					if (!this.IsVerticalByView(pipe))
					{
						YJKRevitTools.SetSystemSimple(this.doc, pipe);
						double tagDis = (pipe.Diameter / 2.0 + (distancToPipe + 125.0) / 304.8) * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
						if (beDelTag)
						{
							YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, pipe, this.szPipeText, false);
							YJKRevitTools.DeleteDLGroups(this.doc, pipe, "标注引线ID", true);
						}
						double num = pipe.GetParameter(BuiltInParameter.CURVE_ELEM_LENGTH).AsDouble();
						double num2 = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
						if (num >= minLength / 304.8 && num2 >= minDia / 304.8 && (!isMarkbyBJ || this.IsPipeConnectBJ(pipe)))
						{
							XYZ hVector = (elementNode.ConnectorOut.Origin - elementNode.ConnectorIn.Origin).Normalize();
							XYZ xyz = YJKRevitTools.GetVectorByHorizontal(hVector);
							XYZ xyz2;
							XYZ xyz3;
							if (elementNode.ConnectorIn != null && elementNode.ConnectorOut != null)
							{
								xyz2 = elementNode.ConnectorIn.Origin;
								xyz3 = elementNode.ConnectorOut.Origin;
							}
							else if (elementNode.ConnectorIn != null)
							{
								xyz2 = elementNode.ConnectorIn.Origin;
								xyz3 = new YJKPiping<Pipe>(pipe).GetDistancePoint(xyz2);
							}
							else if (elementNode.ConnectorOut != null)
							{
								xyz3 = elementNode.ConnectorOut.Origin;
								xyz2 = new YJKPiping<Pipe>(pipe).GetDistancePoint(xyz3);
							}
							else
							{
								YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
								xyz2 = hypiping.StartConnector.Origin;
								xyz3 = hypiping.EndConnector.Origin;
							}
							if (YJKRevitTools.JudgePointLineSite(xyz2 + 2.0 * xyz, xyz2, xyz3) != "左")
							{
								xyz = -1.0 * xyz;
							}
							XYZ xyz4 = new XYZ(0.0, 1.0, 0.0);
							if (xyz.AngleTo(xyz4) > xyz.AngleTo(-xyz4))
							{
								xyz = -1.0 * xyz;
							}
							foreach (XYZ pnt in this.GetTagPoint(elementNode, tagDis, hVector, xyz, upMiddleDown))
							{
								IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), false, 0, 0, pnt);
								independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName, "HY_水管标注")));
								list2.Add(independentTag);
							}
						}
					}
				}
				if (list2.Count == 0)
				{
					YJKMessageBox.Information("管线未被标注！\n搜索到的管线，管长或管径小于最小标注值！");
				}
				result = list2;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public void MarkMultiplyPipe(string tagName, double distanceToTag, bool drawSlash, string upMiddleDown, bool isMarkMultiDisance, double markMultiDistance, bool isDelete)
		{
			try
			{
				XYZ azimuthPoint = new XYZ();
				IList<Pipe> pipeByTwoPoint = this.GetPipeByTwoPoint(out azimuthPoint);
				if (pipeByTwoPoint.Count <= 0)
				{
					YJKMessageBox.Show("没有选中水管，请重新选择……");
					pipeByTwoPoint = this.GetPipeByTwoPoint(out azimuthPoint);
				}
				IList<Pipe> parallelPipe = this.GetParallelPipe(pipeByTwoPoint);
				this.SetPipeParam();
				List<Pipe> list = new List<Pipe>();
				list = (from e in parallelPipe
				orderby (e.Location as LocationCurve).Curve.Project(azimuthPoint).XYZPoint.DistanceTo(azimuthPoint)
				select e).ToList<Pipe>();
				XYZ xyzpoint = (list.ElementAt(0).Location as LocationCurve).Curve.Project(azimuthPoint).XYZPoint;
				XYZ xyz = (azimuthPoint - new XYZ(xyzpoint.X, xyzpoint.Y, azimuthPoint.Z)).Normalize();
				if (isMarkMultiDisance)
				{
					azimuthPoint = xyzpoint + xyz * (markMultiDistance / 304.8);
				}
				Dictionary<Pipe, double> pipeListByDistance = this.GetPipeListByDistance(parallelPipe, azimuthPoint);
				double num = 1.4763779527559056;
				int num2 = 1;
				XYZ xyz2 = new XYZ();
				IndependentTag independentTag = null;
				IList<IndependentTag> list2 = new List<IndependentTag>();
				ElementSet elementSet = new ElementSet();
				new List<DetailLine>();
				XYZ xyz3 = new XYZ();
				new XYZ();
				int num3 = 1;
				int num4 = 0;
				foreach (KeyValuePair<Pipe, double> keyValuePair in pipeListByDistance)
				{
					Pipe key = keyValuePair.Key;
					YJKRevitTools.SetSystemSimple(this.doc, key);
					if (isDelete)
					{
						YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, key, this.szPipeText, false);
						YJKRevitTools.DeleteDLGroups(this.doc, key, "标注引线ID", true);
					}
					if (num3 == 1)
					{
						xyz2 = azimuthPoint;
						independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(key), false, 0, 0, xyz2);
						independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName)));
						int stringByteLength = StringTools.GetStringByteLength(independentTag.TagText);
						if (stringByteLength >= num4)
						{
							num4 = stringByteLength;
						}
					}
					else
					{
						xyz2 += num * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * xyz;
						xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
						independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(key), false, 0, 0, xyz2);
						independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName)));
						int stringByteLength = StringTools.GetStringByteLength(independentTag.TagText);
						if (stringByteLength >= num4)
						{
							num4 = stringByteLength;
						}
					}
					num3++;
				}
				foreach (KeyValuePair<Pipe, double> keyValuePair2 in pipeListByDistance)
				{
					Pipe key2 = keyValuePair2.Key;
					if (num2 == 1)
					{
						this.GetVerticalDistane(key2, azimuthPoint);
						xyz2 = azimuthPoint;
						this.GetPositiveHVector(key2);
						foreach (DetailLine detailLine in this.DrawTagLeadLine(key2, xyz2, xyz, (double)num4, drawSlash, upMiddleDown, tagName))
						{
							elementSet.Insert(detailLine);
						}
						xyz3 = this.GetLeadLinePoint(key2, xyz2, xyz, (double)num4, upMiddleDown, tagName)[0];
						list2.Add(independentTag);
					}
					else
					{
						xyz2 += num * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * xyz;
						xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
						this.GetPositiveHVector(key2);
						IEnumerable<DetailLine> enumerable = this.DrawTagLeadLine(key2, xyz2, xyz, (double)num4, drawSlash, upMiddleDown, tagName);
						xyz3 = this.GetLeadLinePoint(key2, xyz2, xyz, (double)num4, upMiddleDown, tagName)[0];
						foreach (DetailLine detailLine2 in enumerable)
						{
							elementSet.Insert(detailLine2);
						}
						list2.Add(independentTag);
					}
					num2++;
				}
				XYZ xyz4 = xyz3;
				xyz4 = new XYZ(xyz4.X, xyz4.Y, 0.0);
				XYZ xyz5 = new YJKPiping<Pipe>(list.ElementAt(list.Count - 1)).GetProject(xyz4);
				xyz5 = new XYZ(xyz5.X, xyz5.Y, 0.0);
				XYZ xyz6 = (xyz5 - xyz4).Normalize();
				xyz5 += xyz6 * 170.0 / 304.8;
				elementSet.Insert(YJKRevitTools.DrawDetailLine(this.doc, xyz4, xyz5));
				Group group = this.doc.CreatYJKGroup(elementSet);
				foreach (KeyValuePair<Pipe, double> keyValuePair3 in pipeListByDistance)
				{
					Parameter parameter = keyValuePair3.Key.GetParameter("标注引线ID");
					string text = parameter.AsString();
					text = text + "*" + group.Id.ToString();
					parameter.Set(text);
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		public void MarkMultiplyPipe(string tagName, double distanceToTag, bool drawSlash, string upMiddleDown, bool isDelete, bool isTagDis)
		{
			try
			{
				XYZ azimuthPoint = new XYZ();
				IList<Pipe> pipeByTwoPoint = this.GetPipeByTwoPoint(out azimuthPoint);
				if (pipeByTwoPoint.Count <= 0)
				{
					YJKMessageBox.Show("没有选中水管，请重新选择……");
					pipeByTwoPoint = this.GetPipeByTwoPoint(out azimuthPoint);
				}
				IList<Pipe> parallelPipe = this.GetParallelPipe(pipeByTwoPoint);
				XYZ xyz = this.sel.PickPoint("请选择标签相对于引线的方向点......");
				this.SetPipeParam();
				List<Pipe> list = new List<Pipe>();
				list = (from e in parallelPipe
				orderby (e.Location as LocationCurve).Curve.Project(azimuthPoint).XYZPoint.DistanceTo(azimuthPoint)
				select e).ToList<Pipe>();
				LocationCurve locationCurve = list.ElementAt(0).Location as LocationCurve;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list.ElementAt(0));
				while (hypiping.GetProject(xyz, true).IsAlmostEqualTo(hypiping.GetProject(azimuthPoint, true)) && !xyz.IsAlmostEqualTo(azimuthPoint))
				{
					YJKMessageBox.Information("选择引线的方向点与之前第二点在管线的同一法向量上，\n请重选！");
					xyz = this.sel.PickPoint("请选择标签相对于引线的方向点......");
				}
				XYZ xyzpoint = locationCurve.Curve.Project(azimuthPoint).XYZPoint;
				XYZ xyz2 = new XYZ();
				if (xyz.IsAlmostEqualTo(azimuthPoint))
				{
					xyz2 = this.GetPositiveHVector(list.ElementAt(0));
				}
				else
				{
					xyz2 = (hypiping.GetProject(xyz, true) - hypiping.GetProject(azimuthPoint, true)).Normalize();
				}
				xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0).Normalize();
				XYZ xyz3 = (azimuthPoint - new XYZ(xyzpoint.X, xyzpoint.Y, azimuthPoint.Z)).Normalize();
				if (isTagDis)
				{
					azimuthPoint = xyzpoint + xyz3 * (distanceToTag / 304.8 + list.ElementAt(0).Diameter / 2.0);
				}
				Dictionary<Pipe, double> pipeListByDistance = this.GetPipeListByDistance(parallelPipe, azimuthPoint);
				double num = 1.6404199475065617;
				XYZ xyz4 = new XYZ();
				IList<IndependentTag> list2 = new List<IndependentTag>();
				ElementSet elementSet = new ElementSet();
				new List<DetailLine>();
				new XYZ();
				new XYZ();
				int num2 = 1;
				int num3 = 0;
				foreach (KeyValuePair<Pipe, double> keyValuePair in pipeListByDistance)
				{
					Pipe key = keyValuePair.Key;
					YJKRevitTools.SetSystemSimple(this.doc, key);
					if (isDelete)
					{
						YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, key, this.szPipeText, false);
						this.DeleteDLGroups(this.doc, key, "标注引线ID");
					}
					IndependentTag independentTag;
					if (num2 == 1)
					{
						xyz4 = azimuthPoint;
						independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(key), false, 0, 0, xyz4);
						independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName, "HY_水管标注")));
						int stringByteLength = StringTools.GetStringByteLength(independentTag.TagText);
						if (stringByteLength >= num3)
						{
							num3 = stringByteLength;
						}
					}
					else
					{
						xyz4 += num * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0) * xyz3;
						xyz4 = new XYZ(xyz4.X, xyz4.Y, 0.0);
						independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(key), false, 0, 0, xyz4);
						independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName, "HY_水管标注")));
						int stringByteLength = StringTools.GetStringByteLength(independentTag.TagText);
						if (stringByteLength >= num3)
						{
							num3 = stringByteLength;
						}
					}
					list2.Add(independentTag);
					num2++;
				}
				num3++;
				azimuthPoint += xyz2 * ((double)(num3 * 70) / 304.8);
				azimuthPoint = this.IsTagStrartInPipeLine(pipeListByDistance, xyz2, xyz3, azimuthPoint, num3);
				YJKPiping<Pipe> hyPipe = new YJKPiping<Pipe>(parallelPipe[0]);
				this.MoveTag(ref list2, azimuthPoint, hyPipe);
				foreach (DetailLine detailLine in ((IEnumerable<DetailLine>)this.DrawTagLine(list2, xyz2, xyz3, num3, drawSlash, upMiddleDown, list)))
				{
					elementSet.Insert(detailLine);
				}
				Group group = this.doc.CreatYJKGroup(elementSet);
				string text = "";
				foreach (KeyValuePair<Pipe, double> keyValuePair2 in pipeListByDistance)
				{
					Parameter parameter = keyValuePair2.Key.GetParameter("标注引线ID");
					string text2 = parameter.AsString();
					if (isDelete)
					{
						text2 = "*" + group.Id.ToString();
					}
					else
					{
						text2 = text2 + "*" + group.Id.ToString();
					}
					parameter.Set(text2);
					text = text + "*" + keyValuePair2.Key.Id.ToString();
				}
				this.SetGroupES(group, text);
			}
			catch (Exception)
			{
				throw;
			}
		}

		private void SetGroupES(Element group, string pipesid)
		{
			Schema schema = Schema.Lookup(new Guid("97F3BD91-70D9-94CE-AD8B-5A4E8F8A8B11"));
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("97F3BD91-70D9-94CE-AD8B-5A4E8F8A8B11"));
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetVendorId("ADSK");
				schemaBuilder.AddSimpleField("PipemarkInfo", typeof(string));
				schemaBuilder.SetSchemaName("PipemarkInfos");
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("PipemarkInfo");
			entity.Set<string>(field, pipesid);
			group.SetEntity(entity);
		}

		private string GetGroupES(Element group)
		{
			Schema schema = Schema.Lookup(new Guid("97F3BD91-70D9-94CE-AD8B-5A4E8F8A8B11"));
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(new Guid("97F3BD91-70D9-94CE-AD8B-5A4E8F8A8B11"));
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetVendorId("ADSK");
				schemaBuilder.AddSimpleField("PipemarkInfo", typeof(string));
				schemaBuilder.SetSchemaName("PipemarkInfos");
				schema = schemaBuilder.Finish();
			}
			Entity entity = group.GetEntity(schema);
			if (!entity.IsValid())
			{
				return "";
			}
			return entity.Get<string>(schema.GetField("PipemarkInfo"));
		}

		private void DeleteDLGroups(Autodesk.Revit.DB.Document doc, Element elem, string paraName)
		{
			Parameter parameter = elem.GetParameter(paraName);
			List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_IOSDetailGroups);
			if (parameter != null && parameter.HasValue)
			{
				List<string> list = parameter.AsString().Split(new char[]
				{
					'*'
				}).ToList<string>();
				foreach (Element element in elementByCategory)
				{
					if (list.Contains(element.Id.ToString()))
					{
						string groupES = this.GetGroupES(element);
						if (groupES == "")
						{
							doc.DeleteElement(element);
							break;
						}
						if (groupES.Split(new char[]
						{
							'*'
						}).ToList<string>().Contains(elem.Id.ToString()))
						{
							doc.DeleteElement(element);
						}
					}
				}
			}
		}

		public IndependentTag MarkSinglePipe3D(string tagName, double distancToPipe, string upMiddleDown, bool isDelete)
		{
			IndependentTag result;
			try
			{
				Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择要标注的水管……");
				Pipe pipe = this.doc.GetElement(reference) as Pipe;
				YJKRevitTools.SetSystemSimple(this.doc, pipe);
				if (isDelete)
				{
					YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, pipe, this.szPipeText, false);
				}
				LocationCurve locationCurve = pipe.Location as LocationCurve;
				XYZ xyzpoint = locationCurve.Curve.Project(reference.GlobalPoint).XYZPoint;
				XYZ xyz = YJKRevitTools.GetVectorByHorizontal((locationCurve.Curve.GetEndPoint(1) - locationCurve.Curve.GetEndPoint(0)).Normalize());
				if (xyzpoint.Y > (xyzpoint + xyz * 2.0).Y)
				{
					xyz = -1.0 * xyz;
				}
				double num = (pipe.Diameter / 2.0 + (distancToPipe + 125.0) / 304.8) * ((double)YJKRevitTools.GetViewPlanScale(this.doc) / 100.0);
				XYZ pnt = null;
				if (upMiddleDown == "上")
				{
					pnt = xyzpoint + num * xyz;
				}
				else if (upMiddleDown == "中")
				{
					pnt = xyzpoint;
				}
				IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), false, 0, 0, pnt);
				independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, tagName, "HY_水管标注")));
				result = independentTag;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		private bool IsPipeConnectBJ(Pipe pipe)
		{
			foreach (object obj in pipe.ConnectorManager.Connectors)
			{
				Connector connector = (Connector)obj;
				if (!connector.IsConnected)
				{
					return true;
				}
				foreach (object obj2 in connector.AllRefs)
				{
					Element owner = ((Connector)obj2).Owner;
					if (!(owner.Id == pipe.Id))
					{
						ConnectorSet connectors = (owner as FamilyInstance).MEPModel.ConnectorManager.Connectors;
						if (connectors.Size == 3 || connectors.Size == 4)
						{
							return true;
						}
						if (connectors.Size == 2)
						{
							double num = 0.0;
							int num2 = 1;
							foreach (object obj3 in connectors)
							{
								Connector connector2 = (Connector)obj3;
								if (num2 != 1 && connector2.Radius != num)
								{
									return true;
								}
								if (connector2.Radius != num)
								{
									num = connector2.Radius;
								}
								num2++;
							}
						}
					}
				}
			}
			return false;
		}

		private bool IsVerticalByView(Pipe pipe)
		{
			Parameter parameter = pipe.GetParameter(BuiltInParameter.CURVE_ELEM_LENGTH);
			double num = 0.0;
			if (parameter.HasValue)
			{
				num = parameter.AsDouble();
			}
			if (num == 0.0)
			{
				return true;
			}
			if (this.uiDoc != null)
			{
				XYZ xyz = this.uiDoc.ActiveView.ViewDirection.Normalize();
				XYZ hyendPoint = (pipe.Location as LocationCurve).Curve.GetEndPoint(0);
				XYZ hyendPoint2 = (pipe.Location as LocationCurve).Curve.GetEndPoint(1);
				XYZ xyz2 = (hyendPoint - hyendPoint2).Normalize();
				if (xyz.IsAlmostEqualTo(xyz2, 0.001) || xyz.IsAlmostEqualTo(-1.0 * xyz2, 0.001))
				{
					return true;
				}
			}
			return false;
		}

		private bool BeStandPipe(Pipe pipe)
		{
			Parameter parameter = pipe.GetParameter(BuiltInParameter.CURVE_ELEM_LENGTH);
			double num = 0.0;
			if (parameter.HasValue)
			{
				num = parameter.AsDouble();
			}
			return num == 0.0 || YJKRevitTools.isRiserPipe(pipe);
		}

		private XYZ IsTagStrartInPipeLine(Dictionary<Pipe, double> dicPipe, XYZ tagVec, XYZ vVector, XYZ tagPoint, int tagLength)
		{
			vVector = vVector.Normalize();
			int viewPlanScale = YJKRevitTools.GetViewPlanScale(this.doc);
			XYZ xyz = tagPoint - (double)(tagLength * 70) / 304.8 * ((double)viewPlanScale / 100.0) * tagVec;
			foreach (KeyValuePair<Pipe, double> keyValuePair in dicPipe)
			{
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(keyValuePair.Key);
				XYZ project = hypiping.GetProject(xyz, true);
				if (hypiping.IsPointOnPipingCurveExtension(project))
				{
					if (project.DistanceTo(hypiping.StartConnector.Origin) > project.DistanceTo(hypiping.EndConnector.Origin))
					{
						xyz += hypiping.EndConnector.Origin - project;
					}
					else
					{
						xyz += hypiping.StartConnector.Origin - project;
					}
				}
			}
			tagPoint = xyz + (double)(tagLength * 70) / 304.8 * ((double)viewPlanScale / 100.0) * tagVec;
			return tagPoint;
		}

		private void MoveTag(ref IList<IndependentTag> listTag, XYZ markPoint, YJKPiping<Pipe> hyPipe)
		{
			foreach (IndependentTag independentTag in listTag)
			{
				XYZ xyz = independentTag.TagHeadPosition;
				XYZ xyz2 = hyPipe.GetProject(markPoint, true) - hyPipe.GetProject(xyz, true);
				xyz += xyz2;
				independentTag.TagHeadPosition = xyz;
			}
		}

		private List<DetailLine> DrawTagLine(IList<IndependentTag> listTag, XYZ tagVec, XYZ vVec, int tagLength, bool drawSlash, string upMiddleDown, List<Pipe> pipes)
		{
			List<DetailLine> list = new List<DetailLine>();
			int viewPlanScale = YJKRevitTools.GetViewPlanScale(this.doc);
			XYZ xyz = new XYZ();
			bool flag = new HYCreatePipeHelper().judgePointIsAnotherLine((pipes[0].Location as LocationCurve).Curve as Line, listTag[0].TagHeadPosition);
			foreach (IndependentTag independentTag in listTag)
			{
				XYZ xyz2 = independentTag.TagHeadPosition;
				if (flag)
				{
					if (upMiddleDown == "下")
					{
						xyz2 += 0.60695538057742782 * ((double)viewPlanScale / 100.0) * vVec;
					}
					else
					{
						xyz2 -= 0.5741469816272966 * ((double)viewPlanScale / 100.0) * vVec;
					}
				}
				else if (upMiddleDown == "下")
				{
					xyz2 -= 0.5741469816272966 * ((double)viewPlanScale / 100.0) * vVec;
				}
				else
				{
					xyz2 += 0.60695538057742782 * ((double)viewPlanScale / 100.0) * vVec;
				}
				XYZ xyz3 = xyz2 - (double)(tagLength * 70) / 304.8 * ((double)viewPlanScale / 100.0) * tagVec;
				XYZ ePoint = xyz2 + (double)(tagLength * 70) / 304.8 * ((double)viewPlanScale / 100.0) * tagVec;
				list.Add(YJKRevitTools.DrawDetailLine(this.doc, xyz3, ePoint));
				xyz = xyz3;
			}
			if (drawSlash)
			{
				foreach (Pipe elem in pipes)
				{
					XYZ xyzpoint = this.GetElementCurve(elem).Project(xyz).XYZPoint;
					XYZ ePoint2 = xyzpoint + (this.GetPositiveVVector(elem) - this.GetPositiveHVector(elem)) * ((double)viewPlanScale / 600.0);
					XYZ sPoint = xyzpoint - (this.GetPositiveVVector(elem) - this.GetPositiveHVector(elem)) * ((double)viewPlanScale / 600.0);
					list.Add(YJKRevitTools.DrawDetailLine(this.doc, sPoint, ePoint2));
				}
			}
			XYZ xyz4 = xyz;
			xyz4 = new XYZ(xyz4.X, xyz4.Y, 0.0);
			XYZ xyz5 = this.GetFarestPipeProjectPiont(pipes, xyz);
			xyz5 = new XYZ(xyz5.X, xyz5.Y, 0.0);
			XYZ xyz6 = (xyz5 - xyz4).Normalize();
			xyz5 += xyz6 * 100.0 / 304.8;
			list.Add(YJKRevitTools.DrawDetailLine(this.doc, xyz4, xyz5));
			return list;
		}

		private XYZ GetFarestPipeProjectPiont(List<Pipe> pipes, XYZ point)
		{
			XYZ xyz = new XYZ();
			if (pipes.Count == 0)
			{
				return null;
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes.ElementAt(0));
			xyz = this.GetElementCurve(hypiping.Piping).Project(point).XYZPoint;
			double num = point.DistanceTo(xyz);
			foreach (Pipe elem in pipes)
			{
				XYZ xyzpoint = this.GetElementCurve(elem).Project(point).XYZPoint;
				if (point.DistanceTo(xyzpoint) > num)
				{
					num = point.DistanceTo(xyzpoint);
					xyz = xyzpoint;
				}
			}
			return xyz;
		}

		public IList<DetailLine> DrawTagLeadLine(Element elem, XYZ markPoint, XYZ vVector, double tagLength, bool drawSlash, string upMiddleDown, string tagName)
		{
			IList<DetailLine> list = new List<DetailLine>();
			XYZ positiveHVector = this.GetPositiveHVector(elem);
			int viewPlanScale = YJKRevitTools.GetViewPlanScale(this.doc);
			XYZ ePoint = this.GetLeadLinePoint(elem, markPoint, vVector, tagLength, upMiddleDown, tagName)[1];
			XYZ xyz = this.GetLeadLinePoint(elem, markPoint, vVector, tagLength, upMiddleDown, tagName)[0];
			list.Add(YJKRevitTools.DrawDetailLine(this.doc, xyz, ePoint));
			if (drawSlash)
			{
				XYZ xyzpoint = this.GetElementCurve(elem).Project(xyz).XYZPoint;
				ePoint = xyzpoint + (vVector - positiveHVector) * ((double)viewPlanScale / 300.0);
				xyz = xyzpoint - (vVector - positiveHVector) * ((double)viewPlanScale / 300.0);
				list.Add(YJKRevitTools.DrawDetailLine(this.doc, xyz, ePoint));
			}
			return list;
		}

		public IList<XYZ> GetLeadLinePoint(Element elem, XYZ markPoint, XYZ vVector, double tagLength, string upMiddleDown, string tagName)
		{
			List<XYZ> list = new List<XYZ>();
			int viewPlanScale = YJKRevitTools.GetViewPlanScale(this.doc);
			XYZ positiveHVector = this.GetPositiveHVector(elem);
			bool flag = new HYCreatePipeHelper().judgePointIsAnotherLine(((elem as Pipe).Location as LocationCurve).Curve as Line, markPoint);
			XYZ xyz;
			if (upMiddleDown == "下")
			{
				if (flag)
				{
					xyz = markPoint + 0.58727034120734911 * ((double)viewPlanScale / 100.0) * vVector;
				}
				else
				{
					xyz = markPoint - 0.58727034120734911 * ((double)viewPlanScale / 100.0) * vVector;
				}
			}
			else if (flag)
			{
				xyz = markPoint - 0.54133858267716528 * ((double)viewPlanScale / 100.0) * vVector;
			}
			else
			{
				xyz = markPoint + 0.54133858267716528 * ((double)viewPlanScale / 100.0) * vVector;
			}
			XYZ xyz2 = xyz + tagLength * 70.0 / 304.8 * ((double)viewPlanScale / 100.0) * positiveHVector;
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			int num = 70;
			if (tagName == "水管系统缩写标注")
			{
				num = 85;
			}
			else if (tagName == "水管类型标高标注" || tagName == "水管类型外径标注" || tagName == "水管类型管径标注")
			{
				num = 70;
			}
			XYZ xyz3 = xyz - tagLength * (double)num / 304.8 * ((double)viewPlanScale / 100.0) * positiveHVector;
			xyz3 = new XYZ(xyz3.X, xyz3.Y, 0.0);
			((IList<XYZ>)list).Insert(0, xyz3);
			((IList<XYZ>)list).Insert(1, xyz2);
			return list;
		}

		public IList<DetailLine> DrawTagLeadLine(Element elem, XYZ markPoint, XYZ vVector, XYZ hVector, double tagLength, bool drawSlash, string upMiddleDown, string tagName)
		{
			IList<DetailLine> list = new List<DetailLine>();
			int viewPlanScale = YJKRevitTools.GetViewPlanScale(this.doc);
			XYZ ePoint = this.GetLeadLinePoint(elem, markPoint, vVector, hVector, tagLength, upMiddleDown, tagName)[1];
			XYZ xyz = this.GetLeadLinePoint(elem, markPoint, vVector, hVector, tagLength, upMiddleDown, tagName)[0];
			list.Add(YJKRevitTools.DrawDetailLine(this.doc, xyz, ePoint));
			if (drawSlash)
			{
				XYZ xyzpoint = this.GetElementCurve(elem).Project(xyz).XYZPoint;
				ePoint = xyzpoint + (vVector - hVector) * ((double)viewPlanScale / 300.0);
				xyz = xyzpoint - (vVector - hVector) * ((double)viewPlanScale / 300.0);
				list.Add(YJKRevitTools.DrawDetailLine(this.doc, xyz, ePoint));
			}
			return list;
		}

		public IList<XYZ> GetLeadLinePoint(Element elem, XYZ markPoint, XYZ vVector, XYZ hVector, double tagLength, string upMiddleDown, string tagName)
		{
			List<XYZ> list = new List<XYZ>();
			int viewPlanScale = YJKRevitTools.GetViewPlanScale(this.doc);
			bool flag = new HYCreatePipeHelper().judgePointIsAnotherLine(((elem as Pipe).Location as LocationCurve).Curve as Line, markPoint);
			XYZ xyz;
			if (upMiddleDown == "下")
			{
				if (flag)
				{
					xyz = markPoint + 0.58727034120734911 * ((double)viewPlanScale / 100.0) * vVector;
				}
				else
				{
					xyz = markPoint - 0.58727034120734911 * ((double)viewPlanScale / 100.0) * vVector;
				}
			}
			else if (flag)
			{
				xyz = markPoint - 0.54133858267716528 * ((double)viewPlanScale / 100.0) * vVector;
			}
			else
			{
				xyz = markPoint + 0.54133858267716528 * ((double)viewPlanScale / 100.0) * vVector;
			}
			XYZ xyz2 = xyz + tagLength * 70.0 / 304.8 * ((double)viewPlanScale / 100.0) * hVector;
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			XYZ xyz3 = xyz - tagLength * 70.0 / 304.8 * ((double)viewPlanScale / 100.0) * hVector;
			xyz3 = new XYZ(xyz3.X, xyz3.Y, 0.0);
			((IList<XYZ>)list).Insert(0, xyz3);
			((IList<XYZ>)list).Insert(1, xyz2);
			return list;
		}

		private IList<Pipe> GetPipeByTwoPoint(out XYZ secPt)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfClass(typeof(Pipe)).ToElements();
			XYZ firPnt = this.sel.PickPoint("该功能标注两点连线所穿过的所有水管。请选择第一个点......");
			XYZ xyz = this.sel.PickPoint("该功能标注两点连线所穿过的所有水管。请选择第二个点......");
			IList<Pipe> list = new List<Pipe>();
			foreach (Element element in enumerable)
			{
				Pipe pipe = element as Pipe;
				if (!pipe.IsHidden(this.doc.ActiveView))
				{
					LocationCurve locationCurve = pipe.Location as LocationCurve;
					XYZ endPoint = locationCurve.Curve.GetEndPoint(0);
					XYZ endPoint2 = locationCurve.Curve.GetEndPoint(1);
					if (YJKRevitTools.IsLineSegmentCross(firPnt, xyz, endPoint, endPoint2))
					{
						list.Add(pipe);
					}
				}
			}
			secPt = xyz;
			return list;
		}

		private List<XYZ> GetPipeEndPoint(Pipe pipe)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (object obj in pipe.ConnectorManager.Connectors)
			{
				XYZ origin = ((Connector)obj).Origin;
				list.Add(origin);
			}
			return list;
		}

		private IList<Pipe> GetParallelPipe(IList<Pipe> pipeList)
		{
			IList<Pipe> list = new List<Pipe>();
			try
			{
				this.GetElementHVector(pipeList[0]);
				foreach (Pipe pipe in pipeList)
				{
					this.GetElementHVector(pipe);
					list.Add(pipe);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		private bool GetPointIsOnSide(IList<Pipe> listElem, XYZ point)
		{
			bool result = true;
			XYZ elementHVector = this.GetElementHVector(listElem[0]);
			string a = "";
			foreach (Pipe pipe in listElem)
			{
				LocationCurve locationCurve = pipe.Location as LocationCurve;
				XYZ xyz = locationCurve.Curve.GetEndPoint(0);
				xyz = new XYZ(xyz.X, xyz.Y, 0.0);
				XYZ xyz2 = locationCurve.Curve.GetEndPoint(1);
				xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
				XYZ xyz3 = (xyz2 - xyz).Normalize();
				if (elementHVector.AngleTo(xyz3) > Math.PI*.5)
				{
					xyz = locationCurve.Curve.GetEndPoint(1);
					xyz2 = locationCurve.Curve.GetEndPoint(0);
				}
				if (a == "")
				{
					a = YJKRevitTools.JudgePointLineSite(point, xyz2, xyz);
				}
				else if (a != YJKRevitTools.JudgePointLineSite(point, xyz2, xyz))
				{
					result = false;
					break;
				}
			}
			return result;
		}

		public Dictionary<Pipe, double> GetPipeListByDistance(IList<Pipe> list, XYZ point)
		{
			Dictionary<Pipe, double> dictionary = new Dictionary<Pipe, double>();
			Dictionary<Pipe, double> dictionary2 = new Dictionary<Pipe, double>();
			foreach (Pipe pipe in list)
			{
				dictionary.Add(pipe, this.GetVerticalDistane(pipe, point));
			}
			foreach (KeyValuePair<Pipe, double> keyValuePair in from c in dictionary
			orderby c.Value descending
			select c)
			{
				dictionary2.Add(keyValuePair.Key, keyValuePair.Value);
			}
			return dictionary2;
		}

		private IndependentTag CreatElementTag(Element elem, XYZ markPoint, string tagName, HYTagOperate.MarkTagType markType)
		{
			FamilySymbol familySymbol = this.GetFamilySymbol("MarkTag", "HY_风口标注.rfa", tagName);
			IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(elem), false, 0, 0, markPoint);
			independentTag.ChangeTypeId(familySymbol.Id);
			return independentTag;
		}

		private FamilySymbol GetFamilySymbol(string typeName, string familyName, string familySymbolName)
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.doc), typeName, familyName);
			Family family = null;
			SubTransaction subTransaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(this.doc, typeof(FamilySymbol), BuiltInCategory.OST_DuctTerminalTags))
				{
					if ((element as FamilySymbol).Family.Name == familyName.Substring(0, familyName.IndexOf(".")) && element.Name == familySymbolName)
					{
						FamilySymbol result2;
						result = (result2 = (element as FamilySymbol));
						return result2;
					}
				}
				subTransaction = new SubTransaction(this.doc);
				subTransaction.Start();
				this.doc.LoadFamily(text, out family);
				subTransaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
                if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			if (family != null)
			{
				foreach (FamilySymbol familySymbol in family.GetSymbols())
				{
					if (familySymbol.Name == familySymbolName)
					{
						result = familySymbol;
						break;
					}
				}
			}
			return result;
		}

		private IndependentTag CreatElementTag(Element elem, XYZ markPoint, string tagName, HYTagOperate.MarkTagType markType, string familyName)
		{
			this.LoadElemTag(markType, tagName);
			IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(elem), false, 0, 0, markPoint);
			independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(markType, tagName, familyName)));
			return independentTag;
		}

		private IList<XYZ> GetTagPoint(ElementNode eNode, double tagDis, XYZ hVector, XYZ vVector, string upMiddleDown)
		{
			List<XYZ> list = new List<XYZ>();
			LocationCurve locationCurve = eNode.Element.Location as LocationCurve;
			XYZ item = null;
			if (upMiddleDown == "上")
			{
				item = locationCurve.Curve.Evaluate(0.5, true) + tagDis * vVector;
			}
			else if (upMiddleDown == "中")
			{
				item = locationCurve.Curve.Evaluate(0.5, true);
			}
			else if (upMiddleDown == "下")
			{
				item = locationCurve.Curve.Evaluate(0.5, true) - tagDis * vVector;
			}
			list.Add(item);
			return list;
		}

		private IList<XYZ> GetTagPoint(ElementNode eNode, double norDistance, double tagDis, XYZ hVector, XYZ vVector, string upMiddleDown)
		{
			List<XYZ> list = new List<XYZ>();
			LocationCurve locationCurve = eNode.Element.Location as LocationCurve;
			XYZ origin = eNode.ConnectorIn.Origin;
			double num = norDistance;
			double length = locationCurve.Curve.Length;
			if (length <= norDistance)
			{
				XYZ item = null;
				if (upMiddleDown == "上")
				{
					item = locationCurve.Curve.Evaluate(0.5, true) + tagDis * vVector;
				}
				else if (upMiddleDown == "中")
				{
					item = locationCurve.Curve.Evaluate(0.5, true);
				}
				else if (upMiddleDown == "下")
				{
					item = locationCurve.Curve.Evaluate(0.5, true) - tagDis * vVector;
				}
				list.Add(item);
			}
			else if (length > norDistance)
			{
				while (length > num)
				{
					XYZ item2 = null;
					if (upMiddleDown == "上")
					{
						item2 = origin + num * hVector + tagDis * vVector;
					}
					else if (upMiddleDown == "中")
					{
						item2 = origin + num * hVector;
					}
					else if (upMiddleDown == "下")
					{
						item2 = origin + num * hVector - tagDis * vVector;
					}
					list.Add(item2);
					num += norDistance;
				}
			}
			return list;
		}

		private IList<XYZ> GetTagPoint(ElementNode eNode, double norDistance, XYZ hVector)
		{
			List<XYZ> list = new List<XYZ>();
			LocationCurve locationCurve = eNode.Element.Location as LocationCurve;
			XYZ origin = eNode.ConnectorIn.Origin;
			double num = norDistance;
			double length = locationCurve.Curve.Length;
			if (length <= norDistance)
			{
				XYZ item = locationCurve.Curve.Evaluate(0.5, true);
				list.Add(item);
			}
			else if (length > norDistance)
			{
				while (length > num)
				{
					XYZ item2 = origin + num * hVector;
					list.Add(item2);
					num += norDistance;
				}
			}
			return list;
		}

		private IList<XYZ> GetTagPoint(ElementNode eNode, XYZ hVector)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ item = (eNode.Element.Location as LocationCurve).Curve.Evaluate(0.5, true);
			list.Add(item);
			return list;
		}

		public XYZ GetElementHVector(Element elem)
		{
			new XYZ();
			Curve elementCurve = this.GetElementCurve(elem);
			XYZ xyz = elementCurve.GetEndPoint(0);
			xyz = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ xyz2 = elementCurve.GetEndPoint(1);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			return (xyz2 - xyz).Normalize();
		}

		public XYZ GetPositiveHVector(Element elem)
		{
			XYZ xyz = new XYZ();
			Curve elementCurve = this.GetElementCurve(elem);
			XYZ xyz2 = elementCurve.GetEndPoint(0);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			XYZ xyz3 = elementCurve.GetEndPoint(1);
			xyz3 = new XYZ(xyz3.X, xyz3.Y, 0.0);
			xyz = (xyz3 - xyz2).Normalize();
			XYZ xyz4 = new XYZ(1.0, 0.0, 0.0);
			if (xyz4.AngleTo(xyz) > xyz4.AngleTo(-1.0 * xyz))
			{
				xyz = -1.0 * xyz;
			}
			else if (Math.Abs(xyz4.AngleTo(xyz) - xyz4.AngleTo(-1.0 * xyz)) < 0.0001 && xyz2.Y > xyz3.Y)
			{
				xyz = -1.0 * xyz;
			}
			return xyz;
		}

		public XYZ GetPositiveVVector(Element elem)
		{
			Curve elementCurve = this.GetElementCurve(elem);
			XYZ xyz = elementCurve.GetEndPoint(0);
			xyz = new XYZ(xyz.X, xyz.Y, 0.0);
			XYZ xyz2 = elementCurve.GetEndPoint(1);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			XYZ xyz3 = YJKRevitTools.GetVectorByHorizontal((xyz2 - xyz).Normalize());
			XYZ xyz4 = new XYZ(0.0, 1.0, 0.0);
			if (xyz4.AngleTo(xyz3) > xyz4.AngleTo(-1.0 * xyz3))
			{
				xyz3 = -1.0 * xyz3;
			}
			else if (Math.Abs(xyz4.AngleTo(xyz3) - xyz4.AngleTo(-1.0 * xyz3)) < 0.001 && xyz.Y > xyz2.Y)
			{
				xyz3 = -1.0 * xyz3;
			}
			return xyz3;
		}

		public Curve GetElementCurve(Element elem)
		{
			return (elem.Location as LocationCurve).Curve;
		}

		public XYZ GetProPointToCurve(Element elem, XYZ point)
		{
			XYZ xyz = new XYZ();
			xyz = this.GetElementCurve(elem).Project(point).XYZPoint;
			return new XYZ(xyz.X, xyz.Y, 0.0);
		}

		public double GetVerticalDistane(Element elem, XYZ point)
		{
			Curve curve = ((elem as Pipe).Location as LocationCurve).Curve;
			XYZ xyz = new XYZ(point.X, point.Y, 0.0);
			XYZ hyendPoint = curve.GetEndPoint(0);
			XYZ hyendPoint2 = curve.GetEndPoint(1);
			return new YJKLine(new XYZ(hyendPoint.X, hyendPoint.Y, 0.0), new XYZ(hyendPoint2.X, hyendPoint2.Y, 0.0)).GetProject(xyz).DistanceTo(xyz);
		}

		public XYZ GetVector(XYZ point, XYZ vec)
		{
			XYZ result = vec;
			XYZ xyz = point.Add(vec * 2.0);
			if (xyz.X > point.X || xyz.Y < point.Y)
			{
				result = -vec;
			}
			return result;
		}

		public List<IndependentTag> MarkSinglePipeSystemText(Pipe pipe, double dDis, string szText, bool bDel, XYZ globPoint)
		{
			List<IndependentTag> result;
			try
			{
				result = this.PipeSystemText(pipe, 10.0, szText, false, bDel, globPoint);
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public List<IndependentTag> MarkSamePipeSystemText(Pipe pipe, double dDis, string szText)
		{
			List<IndependentTag> result;
			try
			{
				string a = pipe.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsValueString();
				List<Element> list = new FilteredElementCollector(this.doc).OfClass(typeof(Pipe)).OfCategory(BuiltInCategory.OST_PipeCurves).ToElements().ToList<Element>();
				List<IndependentTag> list2 = new List<IndependentTag>();
				foreach (Element element in list)
				{
					Pipe pipe2 = element as Pipe;
					string b = pipe2.GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsValueString();
					if (a == b)
					{
						List<IndependentTag> list3 = this.PipeSystemText(pipe2, dDis, szText, false, true, null);
						if (list3 != null && list3.Count > 0)
						{
							list2.AddRange(list3);
						}
					}
				}
				result = list2;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public List<IndependentTag> MarkAllPipeSystemText(double dDis)
		{
			List<IndependentTag> result;
			try
			{
				List<Element> list = new FilteredElementCollector(this.doc).OfClass(typeof(Pipe)).OfCategory(BuiltInCategory.OST_PipeCurves).ToElements().ToList<Element>();
				List<IndependentTag> list2 = new List<IndependentTag>();
				foreach (Element element in list)
				{
					List<IndependentTag> list3 = this.PipeSystemText(element as Pipe, dDis, null, true, true, null);
					if (list3 != null && list3.Count > 0)
					{
						list2.AddRange(list3);
					}
				}
				result = list2;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		private List<IndependentTag> PipeSystemText(Pipe pe, double dDis, string szText, bool bIsAllView, bool bDel, XYZ globPoint = null)
		{
			List<IndependentTag> list = new List<IndependentTag>();
			if (pe == null || this.IsVerticalByView(pe))
			{
				return list;
			}
			dDis /= 304.8;
			if (szText != null && !string.IsNullOrEmpty(szText))
			{
				this.doc.GetElementById(pe.MEPSystem.GetTypeId()).get_Parameter(BuiltInParameter.RBS_SYSTEM_ABBREVIATION_PARAM).Set(szText);
				this.doc.Regenerate();
			}
			YJKRevitTools.SetSystemSimple(this.doc, pe);
			if (bDel)
			{
				YJKRevitTools.DeleteTagOfElement(this.doc, BuiltInCategory.OST_PipeTags, pe, this.szPipeText, true);
			}
			Curve curve = (pe.Location as LocationCurve).Curve;
			XYZ hyendPoint = curve.GetEndPoint(0);
			XYZ hyendPoint2 = curve.GetEndPoint(1);
			double num = hyendPoint.DistanceTo(hyendPoint2);
			XYZ xyz = (hyendPoint2 - hyendPoint).Normalize();
			XYZ xyz2 = YJKRevitTools.GetVectorByHorizontal(xyz);
			if (hyendPoint.Y > (hyendPoint + xyz2 * 2.0).Y)
			{
				xyz2 *= -1.0;
			}
			if (num <= dDis)
			{
				return list;
			}
			double num2 = (num % dDis + dDis) / 2.0;
			List<XYZ> list2 = new List<XYZ>();
			if (globPoint == null)
			{
				list2.Add(hyendPoint + num2 * xyz);
				while (num2 + dDis < num)
				{
					num2 += dDis;
					list2.Add(hyendPoint + num2 * xyz);
				}
			}
			else
			{
				list2.Add(curve.Project(globPoint).XYZPoint);
			}
			if (bIsAllView)
			{
				using (List<Element>.Enumerator enumerator = new FilteredElementCollector(this.doc).OfClass(typeof(ViewPlan)).ToElements().ToList<Element>().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element = enumerator.Current;
						if (element is ViewPlan)
						{
							ViewPlan viewPlan = element as ViewPlan;
							if ((int)viewPlan.ViewType == 1 && !viewPlan.IsTemplate && this.IsInView(this.doc, viewPlan, pe))
							{
								foreach (XYZ pnt in list2)
								{
									IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, viewPlan, new Reference(pe), false, 0, 0, pnt);
									independentTag.ChangeTypeId(new ElementId(this.GetElemTagElementId(HYTagOperate.MarkTagType.pipe, this.szPipeText, "HY_水管标注")));
									list.Add(independentTag);
								}
							}
						}
					}
					return list;
				}
			}
			foreach (XYZ pnt2 in list2)
			{
				IndependentTag independentTag2 = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pe), false, 0, 0, pnt2);
				independentTag2.ChangeTypeId(new ElementId(this.GetElemTagElementIdOvver(HYTagOperate.MarkTagType.pipe, this.szPipeText, "HY_水管标注")));
				list.Add(independentTag2);
			}
			return list;
		}

		private bool IsInView(Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View v, Element ent)
		{
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_PipeCurves);
			IEnumerator<Element> enumerator = new FilteredElementCollector(doc, v.Id).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements().GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Id.IntegerValue.Equals(ent.Id.IntegerValue))
					{
						return true;
					}
				}
			}
			return false;
		}

		private ExternalCommandData exCmd;

		private UIApplication uiApp;

		private UIDocument uiDoc;

		private Document doc;

		private Selection sel;

		private string pathName = Product.InstallLocation + "\\Data\\WallData.txt";

		private string ductSetPath = Product.UserDataLocation + "\\HYMarkCheck.xml";

		public string imagePath = Product.InstallLocation + "\\Res\\";

		private XmlDocument xmlDoc;

		private string szPipeText = "管上文字";

		public enum MarkTagType
		{
			duct,
			pipe,
			terminal
		}
	}
}
